package com.design.pattern.iterator;

public class MyListIterator<T> implements CustomIterator<T> {
    private  int pos=0;
    private CustomCollection collection;
    private boolean first=true;
    public MyListIterator(CustomCollection collection) {
        this.collection=collection;
    }

    @Override
    public T previous() {
        if (pos<0){
            throw new IndexOutOfBoundsException();
        }
        return (T)collection.indexOf(pos--);
    }

    @Override
    public boolean hasNext() {
        return pos<collection.size()?true:false;
    }

    @Override
    public boolean hasPrevious() {
        if (first){
            pos=collection.size()-1;
            first=false;
        }
        return pos>=0?true:false;
    }

    @Override
    public T next() {
        if (pos+1>collection.size()){
            throw new IndexOutOfBoundsException();
        }
        return (T)collection.indexOf(pos++);
    }

    @Override
    public boolean remove() {
        return collection.remove(collection.indexOf(pos-1));
    }

}
